package com.softer.chengxin.excel.service.impl;

import com.softer.chengxin.common.domin.Common;
import com.softer.chengxin.common.domin.JsonResult;
import com.softer.chengxin.common.domin.ManagerPageResult;
import com.softer.chengxin.common.domin.Page;
import com.softer.chengxin.common.enums.InfoTypeEnum;
import com.softer.chengxin.common.enums.LevelEnum;
import com.softer.chengxin.common.enums.NumberEnum;
import com.softer.chengxin.common.enums.StatisticalReportEnum;
import com.softer.chengxin.common.util.DateFormatUtil;
import com.softer.chengxin.corp.dao.CorpDao;
import com.softer.chengxin.corp.dao.ManagerDao;
import com.softer.chengxin.excel.dao.ReportDao;
import com.softer.chengxin.excel.domin.ExportExcel;
import com.softer.chengxin.excel.domin.ListVO;
import com.softer.chengxin.excel.service.ReportService;
import com.softer.chengxin.excel.service.StatisticalReportService;
import com.softer.chengxin.scheduled.dao.ScheduledTaskDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Gong Zhiyang
 * @date 2019/2/27
 */
@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private ReportDao reportDao;
    @Autowired
    private ManagerDao managerDao;
    @Autowired
    private CorpDao corpDao;
    @Autowired
    private ScheduledTaskDao scheduledTaskDao;
    @Autowired
    private StatisticalReportService statisticalReportService;

    /**
     * 统计报表的导出
     *
     * @param exportExcel 统计报表查询实体
     * @param page        页面信息
     * @param loginName   登录名
     * @return 结果集
     */
    @Override
    public ManagerPageResult getReport(ExportExcel exportExcel, Page page, String loginName) {
        //如果是市部门，查询各个区信息
        //如果是区部门，查询各个街道信息
        Integer roleId = managerDao.getRoleId(loginName);
        ManagerPageResult mpr = new ManagerPageResult();
        JsonResult jr = new JsonResult();
        ListVO listVO = new ListVO();
        String fields = exportExcel.getFields();
        String area = null;
        if (!Common.ADMIN.equals(loginName)) {
            area = corpDao.getDistinct(loginName);
        }
        if (StringUtils.isBlank(fields)) {
            commonResult(mpr, jr, new ArrayList(), Integer.parseInt(Common.ZERO));
            return mpr;
        }
        //获取最近设置的定时任务
        String time = scheduledTaskDao.getTime();
        //获取主管部门定义的年份
        String year = DateFormatUtil.getYear(time);
        exportExcel.setYear(year);
        if (fields.contains(StatisticalReportEnum.CORP_NUM.getField())) {
            listVO.setList1(reportDao.getCorpNum(fields, area, roleId, exportExcel));
        }
        //2项目数量
        if (fields.contains(StatisticalReportEnum.PROJECT_NUM.getField())) {
            listVO.setList2(reportDao.getProjectNum(fields, area, roleId, exportExcel));
        }
        //3通过三体系认证数量
        if (fields.contains(StatisticalReportEnum.THREE_SYSTEM.getField())) {
            listVO.setList3(reportDao.getThreeSystemNum(fields, area, roleId, exportExcel, Common.ONE, StatisticalReportEnum.THREE_SYSTEM.getField()));
        }
        //4未通过三体系认证数量
        if (fields.contains(StatisticalReportEnum.NO_THREE_SYSTEM.getField())) {
            listVO.setList4(reportDao.getThreeSystemNum(fields, area, roleId, exportExcel, Common.ZERO, StatisticalReportEnum.NO_THREE_SYSTEM.getField()));
        }
        //5成立党支部数量
        if (fields.contains(StatisticalReportEnum.ORGANIZATION.getField())) {
            listVO.setList5(reportDao.getOrganizationNum(fields, area, roleId, exportExcel, Common.ONE, StatisticalReportEnum.ORGANIZATION.getField()));
        }
        //6未成立党支部数量
        if (fields.contains(StatisticalReportEnum.NO_ORGANIZATION.getField())) {
            listVO.setList6(reportDao.getOrganizationNum(fields, area, roleId, exportExcel, Common.ZERO, StatisticalReportEnum.NO_ORGANIZATION.getField()));
        }
        //7主营收入
        if (fields.contains(StatisticalReportEnum.MAIN_INCOME.getField())) {
            listVO.setList7(reportDao.getIncome(fields, area, roleId, exportExcel, StatisticalReportEnum.MAIN_INCOME.getField()));
        }
        //8公共收益
        if (fields.contains(StatisticalReportEnum.PUBLIC_INCOME.getField())) {
            listVO.setList8(reportDao.getIncome(fields, area, roleId, exportExcel, StatisticalReportEnum.PUBLIC_INCOME.getField()));
        }
        //9利润
        if (fields.contains(StatisticalReportEnum.PROFIT.getField())) {
            listVO.setList9(reportDao.getIncome(fields, area, roleId, exportExcel, StatisticalReportEnum.PROFIT.getField()));
        }
        //10其他收入
        if (fields.contains(StatisticalReportEnum.OTHER_INCOME.getField())) {
            listVO.setList10(reportDao.getIncome(fields, area, roleId, exportExcel, StatisticalReportEnum.OTHER_INCOME.getField()));
        }
        //11企业总人数
        if (fields.contains(StatisticalReportEnum.CORP_ALL_PERSON_NUM.getField())) {
            listVO.setList11(reportDao.getCorpPersonNum(fields, area, roleId, exportExcel, StatisticalReportEnum.CORP_ALL_PERSON_NUM.getField()));
        }
        //12企业交五险一金人数
        if (fields.contains(StatisticalReportEnum.CORP_INSURANCE.getField())) {
            listVO.setList12(reportDao.getCorpPersonNum(fields, area, roleId, exportExcel, StatisticalReportEnum.CORP_INSURANCE.getField()));
        }
        //13企业持有相关中等从业资格证书的人数
        if (fields.contains(StatisticalReportEnum.CORP_CERTIFICATE.getField())) {
            listVO.setList13(reportDao.getCorpPersonNum(fields, area, roleId, exportExcel, StatisticalReportEnum.CORP_CERTIFICATE.getField()));
        }
        //14项目总人数
        if (fields.contains(StatisticalReportEnum.PROJECT_ALL_PERSON_NUM.getField())) {
            listVO.setList14(reportDao.getProjectPersonNum(fields, area, roleId, exportExcel, StatisticalReportEnum.PROJECT_ALL_PERSON_NUM.getField()));
        }
        //15项目交五险一金人数
        if (fields.contains(StatisticalReportEnum.PROJECT_INSURANCE.getField())) {
            listVO.setList15(reportDao.getProjectPersonNum(fields, area, roleId, exportExcel, StatisticalReportEnum.PROJECT_INSURANCE.getField()));
        }
        //16项目持有相关中等从业资格证书的人数
        if (fields.contains(StatisticalReportEnum.PROJECT_CERTIFICATE.getField())) {
            listVO.setList16(reportDao.getProjectPersonNum(fields, area, roleId, exportExcel, StatisticalReportEnum.PROJECT_CERTIFICATE.getField()));
        }
        //17 D级
        if (fields.contains(StatisticalReportEnum.LEVEL_D.getField())) {
            listVO.setList17(reportDao.getLevelNum(fields, area, roleId, exportExcel, StatisticalReportEnum.LEVEL_D.getField(), LevelEnum.D.getLevel()));
        }
        //18 A级
        if (fields.contains(StatisticalReportEnum.LEVEL_A.getField())) {
            listVO.setList18(reportDao.getLevelNum(fields, area, roleId, exportExcel, StatisticalReportEnum.LEVEL_A.getField(), LevelEnum.A.getLevel()));
        }
        //19 AA级
        if (fields.contains(StatisticalReportEnum.LEVEL_AA.getField())) {
            listVO.setList19(reportDao.getLevelNum(fields, area, roleId, exportExcel, StatisticalReportEnum.LEVEL_AA.getField(), LevelEnum.AA.getLevel()));
        }
        //20 AAA级
        if (fields.contains(StatisticalReportEnum.LEVEL_AAA.getField())) {
            listVO.setList20(reportDao.getLevelNum(fields, area, roleId, exportExcel, StatisticalReportEnum.LEVEL_AAA.getField(), LevelEnum.AAA.getLevel()));
        }
        //21 黑名单数量
        if (fields.contains(StatisticalReportEnum.BLACK_LIST.getField())) {
            listVO.setList21(reportDao.getBlackList(fields, area, roleId, exportExcel, StatisticalReportEnum.BLACK_LIST.getField()));
        }
        //22 项目总占地面积
        if (fields.contains(StatisticalReportEnum.PROJECT_ALL_AREA.getField())) {
            listVO.setList22(reportDao.getProjectArea(fields, area, roleId, exportExcel, StatisticalReportEnum.PROJECT_ALL_AREA.getField(), "project_area"));
        }
        //23 地上建筑面积
        if (fields.contains(StatisticalReportEnum.GROUND_AREA.getField())) {
            listVO.setList23(reportDao.getProjectArea(fields, area, roleId, exportExcel, StatisticalReportEnum.GROUND_AREA.getField(), "building_land"));
        }
        //24 地下建筑面积
        if (fields.contains(StatisticalReportEnum.UNDERGROUND_AREA.getField())) {
            listVO.setList24(reportDao.getProjectArea(fields, area, roleId, exportExcel, StatisticalReportEnum.UNDERGROUND_AREA.getField(), "green_area"));
        }
        //25 住宅建筑面积
        if (fields.contains(StatisticalReportEnum.RESIDENTIAL_AREA.getField())) {
            listVO.setList25(reportDao.getProjectArea(fields, area, roleId, exportExcel, StatisticalReportEnum.RESIDENTIAL_AREA.getField(), "public_facilities_area"));
        }
        //26 非住宅建筑面积
        if (fields.contains(StatisticalReportEnum.NON_RESIDENTIAL_AREA.getField())) {
            listVO.setList26(reportDao.getProjectArea(fields, area, roleId, exportExcel, StatisticalReportEnum.NON_RESIDENTIAL_AREA.getField(), "other_area"));
        }
        //27 绿化面积
        if (fields.contains(StatisticalReportEnum.GREEN_AREA.getField())) {
            listVO.setList27(reportDao.getProjectArea(fields, area, roleId, exportExcel, StatisticalReportEnum.GREEN_AREA.getField(), "municipal"));
        }
        //28 公建配套用房面积
        if (fields.contains(StatisticalReportEnum.PUBLIC_AREA.getField())) {
            listVO.setList28(reportDao.getProjectArea(fields, area, roleId, exportExcel, StatisticalReportEnum.PUBLIC_AREA.getField(), "all_area"));
        }
        //29 住宅户数
        if (fields.contains(StatisticalReportEnum.HOUSEHOLDS.getField())) {
            listVO.setList29(reportDao.getProjectArea(fields, area, roleId, exportExcel, StatisticalReportEnum.HOUSEHOLDS.getField(), "housing_quantity"));
        }
        //30 居住人数
        if (fields.contains(StatisticalReportEnum.NUMBER.getField())) {
            listVO.setList30(reportDao.getProjectArea(fields, area, roleId, exportExcel, StatisticalReportEnum.NUMBER.getField(), "number_residents"));
        }
        //31 老旧小区数量
        if (fields.contains(StatisticalReportEnum.OLD_DISTRICT.getField())) {
            listVO.setList31(reportDao.getHouseNum(fields, area, roleId, exportExcel, StatisticalReportEnum.OLD_DISTRICT.getField()));
        }
        //32 智能化管理小区数量
        if (fields.contains(StatisticalReportEnum.INTELLIGENT.getField())) {
            listVO.setList32(reportDao.getHouseNum(fields, area, roleId, exportExcel, StatisticalReportEnum.INTELLIGENT.getField()));
        }
        //33 保障住房数量
        if (fields.contains(StatisticalReportEnum.INDEMNIFICATORY.getField())) {
            listVO.setList33(reportDao.getHouseNum(fields, area, roleId, exportExcel, StatisticalReportEnum.INDEMNIFICATORY.getField()));
        }
        //34 业委会数量
        if (fields.contains(StatisticalReportEnum.PROPRIETOR.getField())) {
            listVO.setList34(reportDao.getProprietor(fields, area, roleId, exportExcel, StatisticalReportEnum.PROPRIETOR.getField()));
        }
        //35 优良信息数量
        if (fields.contains(StatisticalReportEnum.GOOD_INFO.getField())) {
            listVO.setList35(reportDao.getInfoNum(fields, area, roleId, exportExcel, StatisticalReportEnum.GOOD_INFO.getField(), InfoTypeEnum.EXCELLENT.getCode()));
        }
        //36 负面信息数量
        if (fields.contains(StatisticalReportEnum.BAD_INFO.getField())) {
            listVO.setList36(reportDao.getInfoNum(fields, area, roleId, exportExcel, StatisticalReportEnum.BAD_INFO.getField(), InfoTypeEnum.NEGATIVE.getCode()));
        }
        //37 项目经理培训数量
        if (fields.contains(StatisticalReportEnum.MANAGE_TRAIN.getField())) {
            listVO.setList37(reportDao.getTrainNum(fields, area, roleId, exportExcel, StatisticalReportEnum.MANAGE_TRAIN.getField(), "subject_type", Common.ONE));
        }
        //38 其他培训数量
        if (fields.contains(StatisticalReportEnum.OTHER_TRAIN.getField())) {
            listVO.setList38(reportDao.getTrainNum(fields, area, roleId, exportExcel, StatisticalReportEnum.OTHER_TRAIN.getField(), "subject_type", String.valueOf(NumberEnum.TWO.getNum())));
        }
        //39 市级及以上物业主管部门培训数量
        if (fields.contains(StatisticalReportEnum.CITY_DEPARTMENT.getField())) {
            listVO.setList39(reportDao.getTrainNum(fields, area, roleId, exportExcel, StatisticalReportEnum.CITY_DEPARTMENT.getField(), "department", Common.ONE));
        }
        //40 区（市）物业主管部门培训数量
        if (fields.contains(StatisticalReportEnum.DISTRICT_DEPARTMENT.getField())) {
            listVO.setList40(reportDao.getTrainNum(fields, area, roleId, exportExcel, StatisticalReportEnum.DISTRICT_DEPARTMENT.getField(), "department", String.valueOf(NumberEnum.TWO.getNum())));
        }
        //41 国家、省、市物业协会培训数量
        if (fields.contains(StatisticalReportEnum.ASSOCIATION.getField())) {
            listVO.setList41(reportDao.getTrainNum(fields, area, roleId, exportExcel, StatisticalReportEnum.ASSOCIATION.getField(), "department", String.valueOf(Common.THREE)));
        }
        //42 培训总数量
        if (fields.contains(StatisticalReportEnum.ALL_TRAIN.getField())) {
            listVO.setList42(reportDao.getTrainNum(fields, area, roleId, exportExcel, StatisticalReportEnum.ALL_TRAIN.getField(), null, null));
        }
        compose(listVO);
        ManagerPageResult result = statisticalReportService.exportStatisticalReport(exportExcel, page, loginName);
        List<Map<String, Object>> ds = result.getJson().getDs();
        listVO.getList1().addAll(ds);
        commonResult(mpr, jr, listVO.getList1(), listVO.getList1().size());
        return mpr;
    }

    private void compose(ListVO listVO) {
        for (int i = 0; i < listVO.getList1().size(); i++) {
            Map<String, Object> map = listVO.getList1().get(i);
            String name = (String) map.get("name");
            //2项目数量
            putValue(listVO.getList2(), map, name, StatisticalReportEnum.PROJECT_NUM.getField());
            //3通过三体系认证数量
            putValue(listVO.getList3(), map, name, StatisticalReportEnum.THREE_SYSTEM.getField());
            //4未通过三体系认证数量
            putValue(listVO.getList4(), map, name, StatisticalReportEnum.NO_THREE_SYSTEM.getField());
            //5成立党支部数量
            putValue(listVO.getList5(), map, name, StatisticalReportEnum.ORGANIZATION.getField());
            //6未成立党支部数量
            putValue(listVO.getList6(), map, name, StatisticalReportEnum.NO_ORGANIZATION.getField());
            //7主营收入
            putValue(listVO.getList7(), map, name, StatisticalReportEnum.MAIN_INCOME.getField());
            //8公共收益
            putValue(listVO.getList8(), map, name, StatisticalReportEnum.PUBLIC_INCOME.getField());
            //9利润
            putValue(listVO.getList9(), map, name, StatisticalReportEnum.PROFIT.getField());
            //10其他收入
            putValue(listVO.getList10(), map, name, StatisticalReportEnum.OTHER_INCOME.getField());
            //11企业总人数
            putValue(listVO.getList11(), map, name, StatisticalReportEnum.CORP_ALL_PERSON_NUM.getField());
            //12企业交五险一金人数
            putValue(listVO.getList12(), map, name, StatisticalReportEnum.CORP_INSURANCE.getField());
            //13企业持有相关中等从业资格证书的人数
            putValue(listVO.getList13(), map, name, StatisticalReportEnum.CORP_CERTIFICATE.getField());
            //14项目总人数
            putValue(listVO.getList14(), map, name, StatisticalReportEnum.PROJECT_ALL_PERSON_NUM.getField());
            //15项目交五险一金人数
            putValue(listVO.getList15(), map, name, StatisticalReportEnum.PROJECT_INSURANCE.getField());
            //16项目持有相关中等从业资格证书的人数
            putValue(listVO.getList16(), map, name, StatisticalReportEnum.PROJECT_CERTIFICATE.getField());
            //D级
            putValue(listVO.getList17(), map, name, StatisticalReportEnum.LEVEL_D.getField());
            //A级
            putValue(listVO.getList18(), map, name, StatisticalReportEnum.LEVEL_A.getField());
            //AA级
            putValue(listVO.getList19(), map, name, StatisticalReportEnum.LEVEL_AA.getField());
            //AAA级
            putValue(listVO.getList20(), map, name, StatisticalReportEnum.LEVEL_AAA.getField());
            //黑名单数量
            putValue(listVO.getList21(), map, name, StatisticalReportEnum.BLACK_LIST.getField());
            //项目总占地面积
            putValue(listVO.getList22(), map, name, StatisticalReportEnum.PROJECT_ALL_AREA.getField());
            //地上建筑面积
            putValue(listVO.getList23(), map, name, StatisticalReportEnum.GROUND_AREA.getField());
            //地下建筑面积
            putValue(listVO.getList24(), map, name, StatisticalReportEnum.UNDERGROUND_AREA.getField());
            //住宅建筑面积
            putValue(listVO.getList25(), map, name, StatisticalReportEnum.RESIDENTIAL_AREA.getField());
            //非住宅建筑面积
            putValue(listVO.getList26(), map, name, StatisticalReportEnum.NON_RESIDENTIAL_AREA.getField());
            //绿化面积
            putValue(listVO.getList27(), map, name, StatisticalReportEnum.GREEN_AREA.getField());
            //公建配套用房面积
            putValue(listVO.getList28(), map, name, StatisticalReportEnum.PUBLIC_AREA.getField());
            //住宅户数
            putValue(listVO.getList29(), map, name, StatisticalReportEnum.HOUSEHOLDS.getField());
            //居住人数
            putValue(listVO.getList30(), map, name, StatisticalReportEnum.NUMBER.getField());
            //老旧小区数量
            putValue(listVO.getList31(), map, name, StatisticalReportEnum.OLD_DISTRICT.getField());
            //智能化管理小区数量
            putValue(listVO.getList32(), map, name, StatisticalReportEnum.INTELLIGENT.getField());
            //保障住房数量
            putValue(listVO.getList33(), map, name, StatisticalReportEnum.INDEMNIFICATORY.getField());
            //业委会数量
            putValue(listVO.getList34(), map, name, StatisticalReportEnum.PROPRIETOR.getField());
            //优良信息数量
            putValue(listVO.getList35(), map, name, StatisticalReportEnum.GOOD_INFO.getField());
            //负面信息数量
            putValue(listVO.getList36(), map, name, StatisticalReportEnum.BAD_INFO.getField());
            //项目经理培训
            putValue(listVO.getList37(), map, name, StatisticalReportEnum.MANAGE_TRAIN.getField());
            //其他培训数量
            putValue(listVO.getList38(), map, name, StatisticalReportEnum.OTHER_TRAIN.getField());
            //市级及以上物业主管部门培训数量
            putValue(listVO.getList39(), map, name, StatisticalReportEnum.CITY_DEPARTMENT.getField());
            //区（市）物业主管部门培训数量
            putValue(listVO.getList40(), map, name, StatisticalReportEnum.DISTRICT_DEPARTMENT.getField());
            //国家、省、市物业协会培训数量
            putValue(listVO.getList41(), map, name, StatisticalReportEnum.ASSOCIATION.getField());
            //培训总数量
            putValue(listVO.getList42(), map, name, StatisticalReportEnum.ALL_TRAIN.getField());
        }
    }

    private void commonResult(ManagerPageResult mpr, JsonResult jr, List list, Integer num) {
        mpr.setCount(num);
        jr.setDs(list);
        mpr.setJson(jr);
    }

    private void putValue(List<Map<String, Object>> list, Map<String, Object> map, String name, String value) {
        if (list != null) {
            for (Map<String, Object> m : list) {
                String n = (String) m.get("name");
                if (name.equals(n)) {
                    map.put(value, m.get(value));
                    break;
                }
            }
        }
    }
}
